//
//  MoreViewController.m
//  项目一TimeMovie
//
//  Created by apple on 16/3/29.
//  Copyright © 2016年 huangmingyu. All rights reserved.
//

#import "MoreViewController.h"

@interface MoreViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
    
    NSString *userName;
    UIImage *userIcon;
}
@property (weak, nonatomic) IBOutlet UILabel *sizeLabel;

@end

@implementation MoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self getDataFromSandBox];
    self.navigationItem.title = @"更多";
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]];
    self.tableView.separatorColor = kTableViewSeColor;
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 150)];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(25, 25, 100, 100);
    [button setImage:userIcon forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button.layer.cornerRadius = 50;
    button.layer.masksToBounds = YES;
    button.tag = 2016;
    
    [myView addSubview:button];
    
    UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 25, 200, 50)];
    myLabel.textColor = [UIColor greenColor];
    myLabel.text = userName;
    
    NSLog(@"%@",myLabel.text);
    [myView addSubview:myLabel];
    
    self.tableView.tableHeaderView = myView;
    
}


- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self readCathchSize];
    
    [self saveDataToSandBox];
}

- (void)readCathchSize {
    
    NSInteger fileSize = [self getSize];
    
    NSLog(@"%ld", fileSize);
    
    self.sizeLabel.text = [NSString stringWithFormat:@"%.2f M",fileSize / 1024.0 / 1024.0];
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    if (indexPath.section == 0 && indexPath.row == 0) {
//        self.sizeLabel.text = @"0.0 M";
//        
//        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
//        [[SDImageCache sharedImageCache] clearDisk];
//    
//    }
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        NSString *string = [NSString stringWithFormat:@"清除缓存%@",self.sizeLabel.text];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"清除缓存" message:string  preferredStyle:UIAlertControllerStyleActionSheet];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            self.sizeLabel.text = @"0.0 M";
            
//            [[SDImageCache sharedImageCache] clearDisk];
            [self clearCaches];
            
        }]];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:NULL]];
        
        [self presentViewController:alert animated:YES completion:NULL];
    }
    

}


- (NSInteger)getSize {
    
    NSInteger fileSize = 0;
    
    NSString *HomePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
    NSDirectoryEnumerator *dirEumetor = [[NSFileManager defaultManager] enumeratorAtPath:HomePath];
    for (NSString *fileName in dirEumetor) {
        
        NSString *filePath = [HomePath stringByAppendingPathComponent:fileName];
        
        NSDictionary *dic = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
        
        fileSize += [dic fileSize];
    }
    
    return fileSize;
}

- (void)clearCaches {
    
    NSString *HomePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
    NSDirectoryEnumerator *dirEumetor = [[NSFileManager defaultManager] enumeratorAtPath:HomePath];
    for (NSString *fileName in dirEumetor) {
        
        NSString *filePath = [HomePath stringByAppendingPathComponent:fileName];
        
//        - (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error NS_AVAILABLE(10_5, 2_0);
        
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
    }
}

- (void)buttonAction:(UIButton *)button {
    
    
    UIImagePickerControllerSourceType  sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = sourceType;
    
    picker.delegate = self;
    
    [self presentViewController:picker animated:YES completion:NULL];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    
    userIcon = image;
    UIButton *button = [self.tableView.tableHeaderView viewWithTag:2016];
    [button setImage:userIcon forState:UIControlStateNormal];
   
    
    [picker dismissViewControllerAnimated:YES completion:NULL];
    
    userName = @"我是大帅哥";
    
    [self saveDataToSandBox];
}



- (void)saveDataToSandBox {
    
    if (userIcon != nil) {
        [[NSUserDefaults standardUserDefaults ] setObject:userName forKey:@"userName"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        NSData *data = UIImageJPEGRepresentation(userIcon, 0.8);
        [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"userIcon"
         ];
        
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

- (void)getDataFromSandBox {
    
    userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    userIcon = [UIImage imageWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"userIcon"]];
    [[NSUserDefaults standardUserDefaults] synchronize];
//     NSLog(@"---------------%@",userIcon);
    NSLog(@"%@",NSHomeDirectory());
}

@end
